package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.*;
import cn.sciento.farm.devicemanager.dao.mapper.SceneMapper;
import cn.sciento.farm.devicemanager.dao.model.SceneDeviceCell;
import cn.sciento.farm.devicemanager.dao.model.SceneForm;
import cn.sciento.farm.devicemanager.sensor.Scene;
import cn.sciento.farm.devicemanager.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SceneServiceImpl implements SceneService {

    @Autowired
    SceneMapper sceneMapper;

    @Autowired
    SceneDeviceRelService sceneDeviceRelService;

    @Autowired
    DeviceService deviceService;

    @Autowired
    CollectionService collectionService;

    @Autowired
    ControllerService controllerService;

    @Override
    public int insert(DevScene scene) {
        return sceneMapper.insert(scene);
    }

    @Override
    public int updateById(DevScene scene) {
        return sceneMapper.updateById(scene);
    }

    @Override
    public int deleteById(Long id) {
        return sceneMapper.deleteById(id);
    }

    @Override
    public SceneForm getById(Long id) {
        List<String> idList = getDeviceIdById(id);
        DevScene scene =  sceneMapper.selectById(id);
        SceneForm sceneForm = new SceneForm();
        BeanUtils.copyProperties(scene,sceneForm);
        sceneForm.setDeviceIdList(idList);
        return sceneForm;
    }

    @Override
    public List<SceneForm> getByLandId(int landId) {
        List<SceneForm> sceneList = sceneMapper.getByLandId(landId);

        return sceneList;
    }

    @Override
    public List<SceneForm> getByAreaId(int areaId) {
        List<SceneForm> sceneList = sceneMapper.getByAreaId(areaId);
        return sceneList;
    }

    @Override
    public List<DevDevice> getDeviceById(Long id) {
        List<DevDevice> devDeviceList = deviceService.getBySceneId(id);
        return devDeviceList;
    }

    @Override
    public List<String> getDeviceIdById(Long id) {
        List<DevSceneDeviceRel> relList = sceneDeviceRelService.getBySceneId(id);
        List<String> idList = relList.stream().map(item->item.getDeviceId()).collect(Collectors.toList());
        return idList;
    }

    @Override
    public int updateDevice(Long id, List<String> idList) {
        int count = 0;
        sceneDeviceRelService.deleteBySceneId(id);
        for(String deviceId : idList){
            DevSceneDeviceRel deviceSceneRel = new DevSceneDeviceRel();
            deviceSceneRel.setDeviceId(deviceId);
            deviceSceneRel.setSceneId(id);
            count += sceneDeviceRelService.insert(deviceSceneRel);
        }
        return count;
    }

    @Override
    public List<DevScene> getByDeviceId(String id) {
        List<DevScene> sceneIdList = sceneMapper.getByDeviceId(id);
        return sceneIdList;
    }

    @Override
    public List<SceneDeviceCell> getDeviceAllById(Long id) {
        List<DevDevice> devDeviceList = getDeviceById(id);
        List<SceneDeviceCell> sceneDeviceCellList = new ArrayList<>();
        for(DevDevice devDevice : devDeviceList){
            SceneDeviceCell sceneDeviceCell = new SceneDeviceCell();
            BeanUtils.copyProperties(devDevice, sceneDeviceCell);
            List<DevController> controllerList = controllerService.getByEui(devDevice.getId());
            sceneDeviceCell.setControllerList(controllerList);
            sceneDeviceCellList.add(sceneDeviceCell);
        }
        return sceneDeviceCellList;
    }
}
